package com.Nuyoah.demo01.common;
/*
*处理传参问题
 */

import com.Nuyoah.demo01.Exceptions.AlreadyExistEmail;
import com.Nuyoah.demo01.Exceptions.BanErrorException;
import com.Nuyoah.demo01.Exceptions.UnBanErrorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.mail.MailSendException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
    // 打印log
    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    // ……

    /**
     * 缺少请求参数异常
     * @param ex HttpMessageNotReadableException
     * @return
     */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public JsonResult handleHttpMessageNotReadableException(
            MissingServletRequestParameterException ex) {
        logger.error("缺少请求参数，{}", ex.getMessage());
        return new JsonResult(HttpStatus.BAD_REQUEST, "缺少必要的请求参数");
    }


    /**
     * 空指针异常
     * @param ex NullPointerException
     * @return
     */
    @ExceptionHandler(NullPointerException.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public JsonResult handleTypeMismatchException(NullPointerException ex) {
        logger.error("空指针异常，{}", ex.getMessage());
        return new JsonResult(HttpStatus.INTERNAL_SERVER_ERROR, "空指针异常了");
    }

    /**
     * 处理违反数据库完整性约束异常
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(DataIntegrityViolationException.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public JsonResult handleDataIntegrityViolationException(DataIntegrityViolationException ex) {
        logger.error("数据库 integrity 错误：", ex);
        return new JsonResult(HttpStatus.INTERNAL_SERVER_ERROR, "数据库 integrity 错误");
    }

    /**
     * 处理违反数据库完整性约束异常
     *
     * @param e
     * @return
     * @ExceptionHandler(value = Exception.class) ExceptionHandler的作⽤是⽤来捕获指定的异常。
     */
    @ExceptionHandler(DuplicateKeyException.class)
    public JsonResult<?> handleDuplicateKeyException(DuplicateKeyException e)
    {
        // 将异常信息转为统⼀返回的结果对象
        return  new JsonResult("数据库已存在该记录");
    }


    /**
     * 系统异常 预期以外异常
     * @param ex
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public JsonResult handleUnexpectedServer(Exception ex) {
        logger.error("系统异常：", ex);
        return new JsonResult(HttpStatus.INTERNAL_SERVER_ERROR, "系统发生异常，请联系管理员");
}

    @ExceptionHandler
    public JsonResult handleException(AlreadyExistEmail ex) {
        ex.printStackTrace();
        return new JsonResult<>("1", "该邮箱已被其他用户使用");
    }

    @ExceptionHandler
    public JsonResult handleException(UnBanErrorException ex) {
        ex.printStackTrace();
        return new JsonResult<>("1", "解封失败");
    }

    @ExceptionHandler
    public JsonResult handleException(BanErrorException ex) {
        ex.printStackTrace();
        return new JsonResult<>("1", "封禁失败");
    }

    /**
     * 邮件发送异常处理
     * @param ex MailSendException
     * @return String
     */
    @ExceptionHandler(MailSendException.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public JsonResult handleMailSendException(MailSendException ex) {
        logger.error("邮件发送失败：", ex);
        return new JsonResult<>(HttpStatus.INTERNAL_SERVER_ERROR, "邮件发送失败，请检查邮箱配置或联系管理员");
    }
}